﻿/* 
 * Shuriken is the legal property of its developers. Please refer to the
 * COPYRIGHT file distributed with this
 * source distribution.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Collections.Generic;
using System.Text;
using Shuriken.Base;
using Shuriken.Core.Utils; 


namespace Shuriken.Core.Store
{
    public class QNAction : IAction, ISearchable
    {
        private IAction m_Action;
        private double m_Rank;
        private Shuriken.Core.Ranking.MutableIndexSet m_Hitmask;
        private string m_SearchTerm;
        private static string DEFAULT_IMAGE = string.Empty;

        static QNAction()
        {
            DEFAULT_IMAGE = ImageStore.GetDefaultActionImage();
        }

        public QNAction(IAction action)
        {
            System.Diagnostics.Debug.Assert(action != null, "Null action passed to QNAction!");
            m_Action = action; 
        }

        #region IAction Members

        public void Execute(Shuriken.Base.ISubject subject)
        {
            m_Action.Execute(subject);
        }

        public bool CanExecuteOn(ISubject subject)
        {
            try
            {
                return m_Action.CanExecuteOn(subject);
            }
            catch(Exception excp)
            {
                string msg = ApplicationEnvironment.Diagnostics.FormatCriticalError(excp);                
                Tracer.WriteError(msg, "Plugin Error");

                return false;
            }
        }
        public Type SubjectType
        {
            get { return m_Action.SubjectType; }
        }

        public bool IsDefault
        {
            get {return m_Action.IsDefault; }
        }

        public string ActionAttribute
        {
            get
            {
                return m_Action.ActionAttribute;
            }
            set
            {
                m_Action.ActionAttribute = value; 
            }
        }

        public bool IsAttributed
        {
            get { return m_Action.IsAttributed; }
        }

        #endregion

        #region IObject Members

        public string Name
        {
            get { return m_Action.Name; }
        }

        public string Description
        {
            get { return m_Action.Description; }
        }

        public string Icon
        {
            get
            {
                if (string.IsNullOrEmpty(m_Action.Icon)) 
                {
                    return QNAction.DEFAULT_IMAGE;
                }

                return m_Action.Icon;
            }
            set
            {
                m_Action.Icon = value;
            }
        }

        #endregion

        #region ISearchable Members

        public double Rank
        {
            get
            {
                return m_Rank;
            }
            set
            {
                m_Rank = value;
            }
        }

        public Shuriken.Core.Ranking.MutableIndexSet HitMask
        {
            get
            {
                return m_Hitmask; 
            }
            set
            {
                m_Hitmask = value; 
            }
        }

        /// <summary>
        /// Last search term used to select this item 
        /// </summary>
        public string SearchTerm
        {
            get
            {
                return m_SearchTerm; 
            }
            set
            {
                m_SearchTerm = value; 
            }
        }

        /// <summary>
        /// Unique id
        /// </summary>
        public string UniqueIdentifier
        {
            get {
                return string.Format("{0}.{1}", m_Action.GetType(), m_Action.Name);
            }
        }

        #endregion
    }
}
